﻿package com.wandian.util
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	/**   
	* @author long
	* @QQ 108232706
	* @version 1.0   
	*/   
	public class CLZSlide extends Sprite
	{
		private var atom:MovieClip;
		private var _bg:MovieClip;
		private var _width:Number=100;
		private var _height:Number=0;
		private var wh:Boolean=true;
		private var _min:int;
		private var _max:int;
		private var _nValue:int;
		private var _step:int;
		private var sub_btn:Object;
		private var add_btn:Object;
		private var nBuyInscale:int;

		public function CLZSlide(_atom:MovieClip, bg:MovieClip, _sub_btn=null, _add_btn=null):void
		{
			this.atom=_atom;
			this._bg=bg;
			this.sub_btn=_sub_btn;
			this.add_btn=_add_btn;
			if (_sub_btn != null)
			{
				this.sub_btn.addEventListener(MouseEvent.CLICK, this.subHandler);
				this.add_btn.addEventListener(MouseEvent.CLICK, this.addHandler);
			}
			this.atom.addEventListener(MouseEvent.MOUSE_DOWN, this.down);
			this.atom.addEventListener(MouseEvent.ROLL_OUT, this.outHandler);
			this.atom.addEventListener(MouseEvent.ROLL_OVER, this.overHandler);
			this._bg.addEventListener(MouseEvent.CLICK, this.clickBgHandler);
			return;
		}

		private function outHandler(event:MouseEvent):void
		{
			this.atom.gotoAndStop("up");
			return;
		}

		private function overHandler(event:MouseEvent):void
		{
			if (event.buttonDown)
			{
				this.atom.gotoAndStop("down");
				event.currentTarget.startDrag(false, new Rectangle(0, 0, this._width, this._height));
				this.atom.stage.addEventListener(MouseEvent.MOUSE_UP, this.up);
				this.atom.stage.addEventListener(MouseEvent.MOUSE_MOVE, this.move);
			}
			else
			{
				this.atom.gotoAndStop("over");
			}
			return;
		}

		public function setMinMax(min:int=0, max:int=100, nBuyInscale:int=0, step:int=0):void
		{
			this.show();
			this._min=min;
			this._max=max;
			this._nValue=min;
			this.nBuyInscale=this._min / 2;
			if (step > 0)
			{
				this._step=step;
			}
			else
			{
				if (this._max <= this._min)
				{
					this._step=this._nValue / 10;
				}
				else
				{
					this._step=int((this._max - this._min) / 10);
				}
			}

			this.subHandler();
			return;
		}

		private function down(event:MouseEvent):void
		{
			this.atom.gotoAndStop("down");
			event.currentTarget.startDrag(false, new Rectangle(0, 0, this._width, this._height));
			this.atom.stage.addEventListener(MouseEvent.MOUSE_UP, this.up);
			this.atom.stage.addEventListener(MouseEvent.MOUSE_MOVE, this.move);
			return;
		}

		private function up(event:MouseEvent):void
		{
			this.atom.stage.removeEventListener(MouseEvent.MOUSE_UP, this.up);
			this.atom.stage.removeEventListener(MouseEvent.MOUSE_MOVE, this.move);
			this.atom.stopDrag();
			return;
		}

		private function subHandler(event:MouseEvent=null):void
		{
			this._nValue=this._nValue - this._step;
			if (this._nValue <= this._min)
			{
				this._nValue=this._min;
			}
			this.contAtomXY();
			return;
		}

		private function addHandler(event:MouseEvent):void
		{
			this._nValue=this._nValue + this._step;
			if (this._nValue >= this._max)
			{
				this._nValue=this._max;
			}
			this.contAtomXY();
			return;
		}

		public function changeSlideText(param1:int):void
		{
			if (this._value <= this._min)
			{
				this._value=this._min;
			}
			if (this._value >= this._max)
			{
				this._value=this._max;
			}
			this.contAtomXY();
			return;
		}

		private function contAtomXY():void
		{
			this.atom.x=(this._value - this._min) * this._width / (this._max - this._min);
			this.valueChange();
			return;
		}

		private function clickBgHandler(event:MouseEvent):void
		{
			if (this.wh)
			{
				this.atom.x=this._bg.parent.mouseX - int(this.atom.width / 2);
			}
			this.checkAtom();
			return;
		}

		private function checkAtom():void
		{
			if (this.wh)
			{
				if (this.atom.x + this.atom.width > this._bg.x + this._bg.width)
				{
					this.atom.x=this._bg.x + this._bg.width - this.atom.width;
				}
				if (this.atom.x <= this._bg.x)
				{
					this.atom.x=this._bg.x;
				}
			}
			this.move();
			return;
		}

		private function move(event:MouseEvent=null):void
		{
			if (this.wh)
			{
				this._value=this._min + (this._max - this._min) * (this.atom.x / this._width);
			}
			else
			{
				this._value=this._min + (this._max - this._min) * this.atom.y / this._height;
			}
			if (this._max - this._value < this._step)
			{
				this._value=this._max;
			}
			this.valueChange();
			return;
		}

		private function valueChange():void
		{
			dispatchEvent(new Event(Event.CHANGE));
			return;
		}

		override public function set width(param1:Number):void
		{
			this._width=param1;
			this._height=0;
			this.wh=true;
			return;
		}

		override public function set height(param1:Number):void
		{
			this._height=param1;
			this._width=0;
			this.wh=false;
			return;
		}

		public function hide():void
		{
			this.atom.mouseEnabled=false;
			this._bg.mouseEnabled=false;
			this.sub_btn.mouseEnabled=false;
			this.add_btn.mouseEnabled=false;
			return;
		}

		private function show():void
		{
			this.atom.mouseEnabled=true;
			this._bg.mouseEnabled=true;
			this.sub_btn.mouseEnabled=true;
			this.add_btn.mouseEnabled=true;
			return;
		}

		public function set _value(param1:int):void
		{
			var _loc_2:*=Math.ceil(param1 / this.nBuyInscale);
			this._nValue=_loc_2 * this.nBuyInscale > this._max ? (this._max) : (_loc_2 * this.nBuyInscale);
			return;
		}

		public function get _value():int
		{
			return this._nValue;
		}

	}
}
